//
//  DetailView.swift
//  willdo
//
//  Created by zz on 3/16/18.
//  Copyright © 2018 will do. All rights reserved.
//

import UIKit

class ReviewDetailView: UIView {

    let scrollView = UIScrollView()
    
    let TopView = UIView()
    let ReviewName = UILabel()
    let EmergentState = UILabel()
    let MarkStar = UIImageView()
    let LeaderLabel = UILabel()
    let LeaderImg = UIImageView()
    let Leader = UIButton()
    let DeadLine = UILabel()
    let ReviewState = UILabel()
    
    let BottomView = UIView()
    let AvatarImage = UIButton()
    let LeaderSpeak = UIButton()
    let StartTime = UILabel()
    let AllFile = UIButton()
    let HistoryMsg = UIButton()
    let ReviewMsg = UILabel()
    
    let cellView = UIView()
    
    private var tmpView:UIView!
    
    let ChangeStateBtn = UIButton()
    
    let replaymsg = UITextField()
    
    var delegate:ReviewDetailViewDelegate?
    private var modle:MyReviewDetailModle!
    init(frame: CGRect, modle:MyReviewDetailModle){
        super.init(frame: frame)
        tmpView = ReviewMsg
        self.modle = modle
        
        addSubview(scrollView)
        scrollView.contentSize = CGSize(width: frame.width, height: frame.height)
        scrollView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }

        scrollView.addSubview(TopView)
        scrollView.addSubview(BottomView)
        setupTopView()
        TopView.backgroundColor = UIColor.white
        TopView.snp.makeConstraints { (make) in
            make.top.left.equalToSuperview()
            make.width.equalTo(frame.width)
            make.height.equalToSuperview().dividedBy(6)
        }
        print(scrollView.frame)
        
        setupBottomView()
        BottomView.backgroundColor = UIColor.white
//        BottomView.snp.makeConstraints { (make) in
//            make.top.equalTo(TopView.snp.bottom).offset(10)
//            make.left.width.equalTo(TopView)
//            make.bottom.equalTo(ReviewMsg.snp.bottom).offset(5)
//        }
        
        if (modle.file != nil){
            setupFileView(files: modle.file!)
        }
        if (modle.ReviewModle.state != .needReceive){
            setupReplayView()
        }
        BottomView.snp.makeConstraints { (make) in
            make.top.equalTo(TopView.snp.bottom).offset(10)
            make.left.width.equalTo(TopView)
            make.bottom.equalTo(tmpView.snp.bottom).offset(5)
        }
        BottomView.layoutIfNeeded()
        tmpView = BottomView
        
        if (modle.ReviewModle.state != .needReceive){
            setupCellView()
        }
        
        setupChangeBtn()
        
    }

    
    
    func setupTopView(){
        TopView.addSubview(ReviewName)
        TopView.addSubview(EmergentState)
        TopView.addSubview(MarkStar)
        TopView.addSubview(LeaderLabel)
        TopView.addSubview(LeaderImg)
        TopView.addSubview(Leader)
        TopView.addSubview(DeadLine)
        TopView.addSubview(ReviewState)
        
        ReviewName.text = modle.ReviewModle.name
        EmergentState.text = modle.emergencyState.rawValue
        MarkStar.backgroundColor = UIColor.black
        LeaderLabel.text = "指派人: "
        LeaderImg.image = UIImage()
        LeaderImg.backgroundColor = UIColor.red
        Leader.setTitle(modle.ReviewModle.member, for: .normal)
        Leader.setTitleColor(UIColor.black, for: .normal)
        DeadLine.text = "截止日期: "+modle.ReviewModle.time
        ReviewState.text = modle.ReviewModle.state.rawValue
        
        ReviewName.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(16)
            make.left.equalToSuperview().offset(16)
            make.height.equalTo(26)
        }
        EmergentState.snp.makeConstraints { (make) in
            make.left.equalTo(ReviewName.snp.right).offset(12)
            make.centerY.equalTo(ReviewName.snp.centerY)
            make.height.equalTo(ReviewName)
        }
        MarkStar.snp.makeConstraints { (make) in
            make.centerY.equalTo(ReviewName)
            make.right.equalToSuperview().offset(-16)
            make.width.height.equalTo(22)
        }
        LeaderLabel.snp.makeConstraints { (make) in
            make.top.equalTo(ReviewName.snp.bottom).offset(8)
            make.left.equalTo(ReviewName)
            make.height.equalTo(24)
        }
        LeaderImg.snp.makeConstraints { (make) in
            make.centerY.equalTo(LeaderLabel)
            make.height.width.equalTo(18)
            make.left.equalTo(LeaderLabel.snp.right).offset(4)
        }
        Leader.snp.makeConstraints { (make) in
            make.centerY.equalTo(LeaderLabel)
            make.left.equalTo(LeaderImg.snp.right).offset(2)
            make.height.equalTo(22)
        }
        DeadLine.snp.makeConstraints { (make) in
            make.left.equalTo(LeaderLabel.snp.left)
            make.top.equalTo(LeaderLabel.snp.bottom).offset(2)
            make.height.equalTo(22)
        }
        ReviewState.snp.makeConstraints { (make) in
            make.right.equalTo(MarkStar)
            make.centerY.equalTo(DeadLine)
            make.height.equalTo(23)
        }
        
    }
    
    func setupBottomView(){
        BottomView.addSubview(AvatarImage)
        BottomView.addSubview(LeaderSpeak)
        BottomView.addSubview(StartTime)
        BottomView.addSubview(AllFile)
        BottomView.addSubview(HistoryMsg)
        BottomView.addSubview(ReviewMsg)
        
//        AvatarImage.backgroundColor = UIColor.orange
        AvatarImage.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(10)
            make.left.equalToSuperview().offset(15)
            make.width.height.equalTo(40)
        }
        
        LeaderSpeak.setTitle(modle.name, for: .normal)
        LeaderSpeak.setTitleColor(UIColor.black, for: .normal)
        LeaderSpeak.snp.makeConstraints { (make) in
            make.top.equalTo(AvatarImage).offset(2)
            make.left.equalTo(AvatarImage.snp.right).offset(8)
            make.height.equalTo(22)
        }
        
        StartTime.text = modle.commitTime
        StartTime.font = UIFont.init(name: "PingFang SC", size: 13)
        StartTime.snp.makeConstraints { (make) in
            make.top.equalTo(LeaderSpeak.snp.bottom).offset(5)
            make.left.equalTo(LeaderSpeak)
        }
        
        HistoryMsg.backgroundColor = UIColor.blue
        HistoryMsg.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-15)
            make.top.equalTo(AvatarImage)
            make.width.height.equalTo(24)
        }
        
        AllFile.backgroundColor = UIColor.green
        AllFile.addTarget(self, action: #selector(ReviewFile), for: .touchUpInside)
        AllFile.snp.makeConstraints { (make) in
            make.right.equalTo(HistoryMsg.snp.left).offset(-12)
            make.top.equalTo(HistoryMsg)
            make.height.width.equalTo(HistoryMsg)
        }
        
        ReviewMsg.text = modle.commitMsg
        ReviewMsg.numberOfLines = 0
        ReviewMsg.textColor = UIColor.black
        ReviewMsg.sizeToFit()
        ReviewMsg.snp.makeConstraints { (make) in
            make.top.equalTo(AvatarImage.snp.bottom).offset(8)
            make.left.equalToSuperview().offset(15)
            make.right.equalToSuperview().offset(-15)
        }
        
    }
    
    func setupFileView(files:[MyReviewFile]){
        //BottomView.addSubview(fileViews)
        
        for file in files{
            let fileView = ReviewFileView(file: file)
            BottomView.addSubview(fileView)
            fileView.snp.makeConstraints({ (make) in
                make.top.equalTo(tmpView.snp.bottom).offset(4)
                make.left.right.equalTo(tmpView)
                make.height.equalTo(80)
            })
            tmpView = fileView
        }
    }
    
    func setupReplayView(){
        BottomView.addSubview(replaymsg)
        replaymsg.placeholder = "回复..."
        replaymsg.layer.borderWidth = 1
        replaymsg.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.top.equalTo(tmpView.snp.bottom).offset(10)
            make.height.equalTo(30)
        }
        tmpView = replaymsg
    }
    
    func setupCellView(){
        addSubview(cellView)
        cellView.backgroundColor = UIColor.white
        cellView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.height.equalTo(80)
            make.top.equalTo(BottomView.snp.bottom).offset(8)
        }
        tmpView = cellView
        
        let subReviewView = ReviewCellView(title: "子任务", onView: self, onClick: #selector(SubReview))
        let ReviewMemberView = ReviewCellView(title: "任务成员", onView: self, onClick: #selector(ReviewMember))
        let sepLine = UIView()
        sepLine.backgroundColor = UIColor.lightGray
        cellView.addSubview(sepLine)
        
        cellView.addSubview(subReviewView)
        cellView.addSubview(ReviewMemberView)
        subReviewView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalToSuperview()
            make.height.equalToSuperview().dividedBy(2)
        }
        sepLine.snp.makeConstraints { (make) in
            make.width.equalToSuperview()
            make.height.equalTo(1)
            make.top.equalTo(subReviewView.snp.bottom)
            make.left.equalToSuperview()
        }
        ReviewMemberView.snp.makeConstraints { (make) in
            make.left.right.equalTo(subReviewView)
            make.top.equalTo(subReviewView.snp.bottom)
            make.height.equalTo(subReviewView)
        }
        
    }
    
    @objc func SubReview(){
        delegate?.subReview()
    }
    
    @objc func ReviewMember(){
        delegate?.ReviewMember()
    }
    
    @objc func ReviewFile(){
        delegate?.ReviewFile()
    }
    
    func setupChangeBtn(){
        
        scrollView.addSubview(ChangeStateBtn)
        var btnTitle:String = "接受任务"
        switch modle.ReviewModle.state {
        case .needReceive:
            btnTitle = "接受任务"
        case .needCommit:
            btnTitle = "提交任务"
        case .needReview:
            btnTitle = "撤销提交"
        case .needArchive:
            btnTitle = "归档任务"
        }
        
        ChangeStateBtn.setTitle(btnTitle, for: .normal)
        ChangeStateBtn.setTitleColor(UIColor.blue, for: .normal)
        ChangeStateBtn.titleLabel?.textAlignment = .center
        ChangeStateBtn.layer.borderWidth = 1
        ChangeStateBtn.snp.makeConstraints { (make) in
            make.top.equalTo(tmpView.snp.bottom).offset(15)
            make.centerX.equalToSuperview()
            make.width.equalToSuperview().offset(-20)
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class ReviewFileView: UIView {
    let shortImg = UIImageView()
    let fileTitle = UILabel()
    let fileSize = UILabel()
    let fileDate = UILabel()
    let fileMember = UILabel()
    let downLoadBtn = UIButton()
    init(file:MyReviewFile) {
        super.init(frame: CGRect.zero)
        self.layer.borderWidth = 1
        
        addSubview(shortImg)
        addSubview(fileTitle)
        addSubview(fileSize)
        addSubview(fileDate)
        addSubview(fileMember)
        addSubview(downLoadBtn)
        
        shortImg.backgroundColor = UIColor.red
        
        fileTitle.text = file.name
        fileSize.text = file.size
        fileSize.font = UIFont.init(name: "PingFang SC", size: 13)
        
        fileDate.text = file.time
        fileMember.text = "来自 " + file.member
        
        downLoadBtn.backgroundColor = UIColor.blue
        
        shortImg.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(10)
            make.height.width.equalTo(60)
        }
        fileTitle.snp.makeConstraints { (make) in
            make.top.equalTo(shortImg).offset(4)
            make.left.equalTo(shortImg.snp.right).offset(4)
        }
        fileSize.snp.makeConstraints { (make) in
            make.left.equalTo(fileTitle)
            make.top.equalTo(fileTitle.snp.bottom).offset(2)
        }
        fileDate.snp.makeConstraints { (make) in
            make.left.equalTo(fileSize.snp.right).offset(4)
            make.top.equalTo(fileSize)
        }
        fileMember.snp.makeConstraints { (make) in
            make.bottom.equalTo(shortImg).offset(-2)
            make.left.equalTo(fileTitle)
        }
        
        downLoadBtn.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-8)
            make.width.height.equalTo(24)
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class ReviewCellView: UIView {
    let titleLabel = UILabel()
    let arrowLable = UILabel()
    let clearBtn = UIButton()
    init(title:String, onView:Any, onClick:Selector) {
        super.init(frame: CGRect.zero)
        addSubview(titleLabel)
        addSubview(arrowLable)
        addSubview(clearBtn)
        titleLabel.text = title
        arrowLable.text = ">"
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(10)
            make.centerY.equalToSuperview()
            make.height.equalToSuperview()
        }
        arrowLable.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-10)
            make.centerY.equalToSuperview()
            make.height.equalTo(titleLabel)
        }
        clearBtn.backgroundColor = UIColor.clear
        clearBtn.addTarget(onView, action: onClick, for: .touchUpInside)
        clearBtn.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

protocol ReviewDetailViewDelegate {
    func receiveBtnClick()
    func commitBtnClick()
    func redoBtnClick()
    func archieveBtnClick()
    func subReview()
    func ReviewMember()
    func ReviewFile()
}


